import datetime
import os

prep = ['in', 'on', 'with', 'by', 'for', 'at', 'about', 'under', 'of', 'to', 'a', 'an']


def _save_(contain, words):
    segment = str(words).strip().split(' ')
    for word in segment:
        if contain.__contains__(word):
            contain[word] += 1
        else:
            contain[word] = 1


def parse_cache():
    with open('./cache/cache.txt', 'r', encoding='utf-8') as fp:
        lines = fp.readlines()

        # 去重
        src = set()

        for line in lines:
            src.add(line)

        # 分析词汇
        contain = {}
        for s in src:
            _save_(contain, s)

        return sorted(contain.items(), key=lambda kv: (kv[1], kv[0]))


def _parse_word_(word):
    if str(word).startswith('$') or str(word).startswith('￥'):
        return 'money'
    if '0' <= str(word)[0] <= '9':
        return 'num'
    if 'A' <= str(word)[0] <= 'Z' and len(word) > 1:
        return 'noun'
    if str(word) in prep:
        return 'prep'
    return 'others'


def _show_table_(dic):
    import matplotlib.pyplot as plt

    plt.rcParams['font.sans-serif'] = ['KaiTi']  # 指定默认字体
    plt.rcParams['axes.unicode_minus'] = False
    # plt.rcParams['font.sans-serif'] = ['Arial Unicode MS'] # Mac下方法

    label = []
    count = []

    for in_k, in_v in dic.items():
        label.append(in_k)
        count.append(in_v)

    plt.pie(count, labels=label, autopct='%1.1f%%', shadow=True, startangle=90)
    plt.axis("equal")
    plt.show()


def _show_table_2_(dic):
    import matplotlib.pyplot as plt

    plt.rcParams['font.sans-serif'] = ['KaiTi']  # 指定默认字体
    plt.rcParams['axes.unicode_minus'] = False
    # plt.rcParams['font.sans-serif'] = ['Arial Unicode MS'] # Mac下方法
    x = []
    y = []
    for k, v in dic.items():
        x.append(k)
        y.append(v)
    plt.bar(x, y, align='center')
    plt.xlabel('高频词汇')
    plt.ylabel('频率/个')
    plt.show()


def _show_table_3_(dic1, dic2):
    import matplotlib.pyplot as plt

    plt.rcParams['font.sans-serif'] = ['KaiTi']  # 指定默认字体
    plt.rcParams['axes.unicode_minus'] = False
    # plt.rcParams['font.sans-serif'] = ['Arial Unicode MS'] # Mac下方法
    x1 = []
    y1 = []
    for k, v in (dic1.items() if isinstance(dic1, dict) else dic1):
        x1.append(k)
        y1.append(v)

    x2 = []
    y2 = []
    for k, v in (dic2.items() if isinstance(dic2, dict) else dic2):
        x2.append(k)
        y2.append(v)

    plt.figure(12)
    plt.subplot(211)
    plt.plot(x1, y1, 'r--')
    plt.subplot(212)
    plt.plot(x2, y2, 'r--')

    plt.show()


def do(timestr):
    today = './data/' + timestr

    data_c = {}
    len_c = {
        '11+': 0
    }
    with open(today + '/count.txt', 'r', encoding='utf-8') as fp:
        lines = fp.readlines()
        for line in lines:
            line = line.replace('\n', '')
            key = line.split(' ')[0]
            value = int(line.split(' ')[1])

            if data_c.__contains__(_parse_word_(key)):
                data_c[_parse_word_(key)] += value
            else:
                data_c[_parse_word_(key)] = value

            if len_c.__contains__(len(key)) and len(key) <= 10:
                len_c[len(key)] += 1
            elif len(key) <= 10:
                len_c[len(key)] = 1
            else:
                len_c['11+'] += 1

    return data_c, len_c


def save():
    today = './data/' + datetime.datetime.now().strftime('%y-%m-%d')

    if not os.path.exists(today):
        os.mkdir(today)

    data = parse_cache()

    with open(today + '/count.txt', 'w', encoding='utf-8') as fp:
        for k, v in data:
            key = str(k).replace('\"', '')
            key = key[1:] if key.startswith('\'') else key
            key = key[:-1] if key.endswith('\'') else key
            if len(key) == 0:
                continue
            fp.write(key)
            fp.write(' ')
            fp.write(str(v))
            fp.write('\n')


def do_special(timestr):
    _show_table_(do(timestr)[0])


def do_length(timestr):
    _show_table_(do(timestr)[1])


def _get_compare_list_(start1, end1, start2, end2):
    ls = os.listdir('./data')
    # 存放符合条件的目录
    com1 = []
    com2 = []

    fmt = '%y-%m-%d'
    d1 = datetime.datetime.strptime(str(start1), fmt)
    d2 = datetime.datetime.strptime(str(end1), fmt)
    d3 = datetime.datetime.strptime(str(start2), fmt)
    d4 = datetime.datetime.strptime(str(end2), fmt)

    for l in ls:
        d = datetime.datetime.strptime(str(l), fmt)
        if d1 <= d <= d2:
            com1.append(l)

        if d3 <= d <= d4:
            com2.append(l)

    # 解析
    dic1 = dict()
    dic2 = dict()

    for com in com1:
        with open('./data/' + str(com) + '/count.txt', 'r', encoding='utf-8') as fp:
            lines = fp.readlines()
            for line in lines:
                k = line.split(' ')[0]
                v = line.split(' ')[1]
                if dic1.__contains__(k):
                    dic1[k] += int(v)
                else:
                    dic1[k] = int(v)

    for com in com2:
        with open('./data/' + str(com) + '/count.txt', 'r', encoding='utf-8') as fp:
            lines = fp.readlines()
            for line in lines:
                k = line.split(' ')[0]
                v = line.split(' ')[1]
                if dic2.__contains__(k):
                    dic2[k] += int(v)
                else:
                    dic2[k] = int(v)
    # 排序
    dc1 = sorted(dic1.items(), key=lambda kv: (kv[1], kv[0]))
    dc2 = sorted(dic2.items(), key=lambda kv: (kv[1], kv[0]))

    return dc1, dc2


def do_compare_top(start1, end1, start2, end2):
    dic1, dic2 = _get_compare_list_(start1, end1, start2, end2)

    top_c = {}
    top_c2 = {}
    cnt = 0
    for k, v in reversed(dic1):
        if _parse_word_(k) != 'noun':
            continue
        top_c[k] = v
        cnt += 1
        if cnt == 10:
            break

    cnt = 0
    for k, v in reversed(dic2):
        if _parse_word_(k) != 'noun':
            continue
        top_c2[k] = v
        cnt += 1
        if cnt == 10:
            break

    _show_table_3_(top_c, top_c2)


def do_compare_special(start1, end1, start2, end2):
    dic1, dic2 = _get_compare_list_(start1, end1, start2, end2)

    data_c = {}
    data_c2 = {}

    for key, value in dic1:
        if data_c.__contains__(_parse_word_(key)):
            data_c[_parse_word_(key)] += value
        else:
            data_c[_parse_word_(key)] = value

    for key, value in dic2:
        if data_c2.__contains__(_parse_word_(key)):
            data_c2[_parse_word_(key)] += value
        else:
            data_c2[_parse_word_(key)] = value
    _show_table_3_(data_c, data_c2)


def do_compare_length(start1, end1, start2, end2):
    dic1, dic2 = _get_compare_list_(start1, end1, start2, end2)

    len_c = {
        '11+': 0
    }

    len_c2 = {
        '11+': 0
    }
    for key, value in dic1:
        if len_c.__contains__(len(key)) and len(key) <= 10:
            len_c[len(key)] += 1
        elif len(key) <= 10:
            len_c[len(key)] = 1
        else:
            len_c['11+'] += 1
    for key, value in dic2:
        if len_c2.__contains__(len(key)) and len(key) <= 10:
            len_c2[len(key)] += 1
        elif len(key) <= 10:
            len_c2[len(key)] = 1
        else:
            len_c2['11+'] += 1

    _show_table_3_(sorted(len_c.items(), key=lambda kv: (kv[0] if isinstance(kv[0], int) else 100)),
                   sorted(len_c2.items(), key=lambda kv: (kv[0] if isinstance(kv[0], int) else 100)))


def do_compare_new(start1, end1, start2, end2):
    dc1, dc2 = _get_compare_list_(start1, end1, start2, end2)
    dc1t = []
    dis = []
    for item in dc1:
        dc1t.append(item[0].split(' ')[0])

    for d in dc2:
        if d[0].split(' ')[0] in dc1t:
            continue
        dis.append(d[0].split(' ')[0])
    return dis


def do_top(timestr):
    today = './data/' + timestr + '/count.txt'

    top_c = {}
    cnt = 0
    with open(today, 'r', encoding='utf-8') as fp:
        lines = fp.readlines()
        for line in reversed(lines):
            k = line.split(' ')[0]
            if _parse_word_(k) != 'noun':
                continue
            v = int(line.split(' ')[1].replace('\n', ''))
            top_c[k] = v
            cnt += 1

            if cnt == 10:
                break
    _show_table_2_(top_c)


def do_new(timestr, oldtimestr):
    today = './data/' + timestr + '/count.txt'
    lastday = './data/' + oldtimestr + '/count.txt'

    t1 = []
    t2 = []
    with open(today, 'r', encoding='utf-8') as fp:
        lines = fp.readlines()
        for line in reversed(lines):
            k = line.split(' ')[0]
            if t1.__contains__(k):
                continue
            t1.append(k)

    with open(lastday, 'r', encoding='utf-8') as fp:
        lines = fp.readlines()
        for line in reversed(lines):
            k = line.split(' ')[0]
            if t1.__contains__(k):
                continue
            t2.append(k)

    return t2
